package com.mrent.web.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mrent.entity.AdminPayment;
import com.mrent.entity.AdminShipping;
import com.mrent.service.AdminShippingService;
import com.mrent.service.Page;
import com.mrent.util.Constance;
import com.mrent.util.RenderUtil;


@Controller
public class AdminShippingController {
	
	@Autowired
	private AdminShippingService adminShippingService;
	
	/**
	 * 配送管理.
	 * 
	 * @return
	 */
	@RequestMapping("/admin/shipping/index")
	public String index(){
		
		return "admin/shipping/home";
	}
	
	
	
	/**
	 * 配送列表.
	 * @return
	 */
	@RequestMapping("/admin/shipping/list")
	public String list(String page,HttpServletResponse response){
		int p = 1;
		Page pp = new Page();
		try{
			p = Integer.parseInt(page);
		}catch(Exception e){
			p = 1;
		}
		pp.setPageNo(p);
		pp.setPageSize(Constance.DEFAULT_PAGE_SIZE);
		
		List<AdminShipping> list = adminShippingService.listShipping(pp);
		
		Map<String,Object> json = new HashMap<String,Object>();
		json.put("rows", list);
		json.put("total", pp.getTotalCount());
		RenderUtil.renderJson(json,response);
		
		return null;
	}
	
	/**
	 * 添加配送方式.
	 * 
	 * @return
	 */
	@RequestMapping("/admin/shipping/add")
	public String add(){
		
		return "admin/shipping/edit";
	}
	
	/**
	 * 修改配送方式.
	 * 
	 * @param payId
	 * @param model
	 * @return
	 */
	@RequestMapping("/admin/shipping/modify/{shippingId}")
	public String modify(@PathVariable Long shippingId,ModelMap model){
		if(shippingId!=null){
			AdminShipping shipping = adminShippingService.getShipping(shippingId);
			model.addAttribute("shipping", shipping);
		}
		return "/admin/shipping/edit";
	}
	
	/**
	 * 保存配送方式.
	 * 
	 * @param adminPayment
	 * @param response
	 * @return
	 */
	@RequestMapping("/admin/shipping/save")
	public String save(AdminShipping adminShipping,HttpServletResponse response){
		if(adminShipping.getShippingId() != null && adminShipping.getShippingId() >0){
			adminShipping.setEnable(new Integer(1));
			adminShippingService.updateShipping(adminShipping);
		}else{
			adminShipping.setEnable(new Integer(1));
			adminShippingService.saveShipping(adminShipping);
		}
		Map<String,Object> json = new HashMap<String,Object>();
		json.put("result", "Success");
		RenderUtil.renderJson(json, response);
		
		return null;
	}
	
	/**
	 * 删除支付方式.
	 * 
	 * @return
	 */
	@RequestMapping("/admin/shipping/del/{shippingId}")
	public String del(@PathVariable Long shippingId,HttpServletResponse response){
		if(shippingId != null){
			AdminShipping adminShipping = adminShippingService.getShipping(shippingId);
			adminShipping.setEnable(new Integer(0));
			adminShippingService.updateShipping(adminShipping);
		}
		Map<String,Object> json = new HashMap<String,Object>();
		json.put("result", "Success");
		RenderUtil.renderJson(json, response);
		return null;
	}

}
